
package nl.rainbowsheep.reisorg.rest.service.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyJoinColumn;
import javax.persistence.PostLoad;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.validation.constraints.NotNull;
import nl.rainbowsheep.reisorg.rest.service.util.Localizer;
import org.springframework.format.annotation.DateTimeFormat;

/**
 */
@Entity
@Table(name = "regions")
public class Region implements Serializable, ILocalized {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "region_id")
    private Long id;

    @Column(name = "name")
    @NotNull
    private String name;

    @Column(name = "fake_description")
    private String description;

    @Column(name = "last_update")
    @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:SS")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date lastUpdate;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "regions_locale", joinColumns = @JoinColumn(name = "regions_fk"))
    @MapKeyJoinColumn(name = "languages_fk")
    private Map<Language, RegionLocalized> locale;

    @Column(name = "country_fk")
    private Long countryId;
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {

        this.description = description;
    }

    @JsonIgnore
    @Override
    public Map getLocale() {
        return locale;
    }

    @Override
    public void setLocale(Map locale) {
        this.locale = locale;
    }


    public Long getCountryId() {
        return countryId;
    }

    public void setCountryId(Long countryId) {
        this.countryId = countryId;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    @Override
    @PostLoad
    @PostUpdate
    public void localize() {
        RegionLocalized localizedObject = (RegionLocalized) Localizer.getLocalizedObject(this.locale, RegionLocalized.class);
        this.description = localizedObject.getDescription();
    }

    @Override
    @PreUpdate
    @PrePersist
    public void parseLocalizedData() {
        RegionLocalized localizedObject = null;
        if (this.locale == null) {
            this.locale = new HashMap<Language, RegionLocalized>();
        }
        if (Localizer.getLocalizedObject(this.locale, RegionLocalized.class) != null) {
            localizedObject = (RegionLocalized) Localizer.getLocalizedObject(this.locale, RegionLocalized.class);
        } else {
            localizedObject = new RegionLocalized();
            this.locale.put(Localizer.getCurrentLanguage(), localizedObject);
        }
        localizedObject.setDescription(this.description);
    }
}
